package com.yc.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author zwei
 * @create 2024-08-26 10:07
 */

@Component
@Slf4j
public class CacheScheduler {

    private final CacheManager cacheManager;

    public CacheScheduler(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    @Scheduled(fixedRate = 60000*10) //定时任务  每隔10分钟清除缓存
    public void clearAllCaches() {
        cacheManager.getCacheNames().forEach(cacheName -> {
            log.info("每隔一分钟清除缓存：" + cacheName);
            cacheManager.getCache(cacheName).clear();
        });
    }
}
